package model

import (
	"github.com/jinzhu/gorm"
	"time"
)

type User struct {
	Id        int        `gorm:"primary_key"json:"id"`
	UserName  string     `gorm:"type:varchar(20);not null;" json:"username"`
	Password  string     `gorm:"type:varchar(255):;not null;" json:"password"`
	Tel       string     `gorm:"type:varchar(120);not null;" json:"tel"`
	Token     string     `gorm:"type:varchar(255);not null;" json:"token"`
	Image     string     `gorm:"type:varchar(255);not null;"json:"image"`
	IsOnline  int        `gorm:"type:int;not null;"        json:"is_online"`
	CreatedAt time.Time  `gorm:"type:datetime;not null;"json:"created_at"`
	UpdatedAt time.Time  `gorm:"type:datetime;not null;"json:"updated_at"`
	DeletedAt *time.Time `gorm:"type:datetime;not null;"json:"deleted_at"`
}
type CharUser struct {
	C        chan string
	Username string `gorm:"type:varchar(20);not null;" json:"username"`
}

func GetUserList(db *gorm.DB) []User {
	var userList []User
	db.Debug().First(&userList)
	return userList
}
func GetUser(db *gorm.DB, tel string) User {
	var user User
	db.Debug().Table("user").Where("tel = ?", tel).First(&user)
	return user
}
func GetToken(db *gorm.DB, id int) (User, error) {
	var user User
	err := db.Debug().Table("user").Where("id = ?", id).First(&user).Error
	return user, err
}

//查询用户信息
func GetUserList1(db *gorm.DB, contine string) ([]User, error) {
	var user []User
	err := db.Table("user").Where(contine).Table("user").Find(&user).Error
	return user, err
}
func UpdateOnlineStatus(db *gorm.DB, status int, id int) {
	db.Debug().Exec("update user set is_online=? where id=?", status, id)
}
func Update(db *gorm.DB, tel string, token string, status int) bool {
	var user User
	user.Token = token
	user.IsOnline = status
	err := db.Debug().Where("tel = ? ", tel).Table("user").Update(&user)
	if err != nil {
		return false
	}
	return true
}

func EmptyToken(db *gorm.DB, id int) {

	db.Debug().Exec("update user set token='' where id=?", id)
}

func IsTelExist(db *gorm.DB, tel string) bool {
	var user User
	db.Table("user").Where("tel = ?", tel).First(&user)
	if user.Id != 0 {
		return true
	}
	return false
}

func IsPasswordExist(db *gorm.DB, tel string, password string) bool {
	var user User
	db.Table("user").Where("tel = ?" + tel + "password" + password).First(&user)
	if user.Id != 0 {
		return true
	}
	return false
}
